﻿// JavaScript Document
var global_n=-1;
var global_introAddr;
var xmlHttp;
var SRBox;
var RPBox
var EDBox;

function active(n,introAddr){
	global_n = n;
	global_introAddr=introAddr;
	SRBox = document.getElementById("SearchResult");
	RPBox = document.getElementById("rightPanel");
	EDBox = document.getElementById("editPanel");
	for(var i=0;i<SRBox.getElementsByTagName("li").length;i++)
		SRBox.getElementsByTagName("li")[i].className="off";
	SRBox.getElementsByTagName("li")[n].className="on";
	if(SRBox.getElementsByTagName("li")[n].innerHTML==""){
		SRBox.getElementsByTagName("li")[n].innerHTML="on";
		}
	for(var i=0;i<RPBox.getElementsByTagName("div").length;i++)
		RPBox.getElementsByTagName("div")[i].style.display="none";
	RPBox.getElementsByTagName("div")[n].style.display="block";
	if(RPBox.getElementsByTagName("div")[n].innerHTML == ""){
		RPBox.style.background="url(./images/loading.gif) center center no-repeat";
		loadingIntro(introAddr);
	}
	RPBox.style.display="block";
	EDBox.style.display="none";
} 

function modify(){
	if(global_n!=-1){
		RPBox.style.display="none";
		EDBox.style.display="block";
		editor.writeDocument(RPBox.getElementsByTagName("div")[global_n].innerHTML);
	}
}

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
    }
}

function loadingIntro(introAddr) {
    createXMLHttpRequest();
	var url = "loading?intro=" + encodeURI(introAddr);
    xmlHttp.onreadystatechange = handleStateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function handleStateChanged() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            result();
        }else{
        	alert("status:"+xmlHttp.status);
        }
    }
}

function result() {
	RPBox = document.getElementById("rightPanel");
    var introString = xmlHttp.responseText;
    if(introString == ""){
		RPBox.getElementsByTagName("div")[global_n].innerHTML=
			SRBox.getElementsByTagName("li")[global_n].getElementsByTagName("div")[0].firstChild.innerHTML
			+ "<br />" +SRBox.getElementsByTagName("li")[global_n].getElementsByTagName("span")[1].innerHTML
			+ "<br />" +SRBox.getElementsByTagName("li")[global_n].getElementsByTagName("span")[3].title;
	}else{
		RPBox.getElementsByTagName("div")[global_n].innerHTML=introString;
	}
    RPBox.style.background="url(./images/logo_alpha.jpg) center center no-repeat";
}


